package com.onpositive.funkytable.test;

import java.util.Date;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.IReadableObjectCollection;

public class FieldWithMeta {

	private IPropertyMetadata meta;
	private IReadableObjectCollection r;

	public FieldWithMeta(final IPropertyMetadata m,
			final IReadableObjectCollection r) {		
		if (m != null) {
			Object value = m.getMetaPropertyValue("parser-id");
			if (value != null && value.equals("dimension")) {
				unit = (String) m.getMetaPropertyValue("unit");
			}
			this.meta = m;
		}
		this.r=r;
	}
	
	public Object getPropertyValue(Object object) {
		Object value = r.getValue(object, meta);
		if (value != null) {
			if (value instanceof String) {
				String s = (String) value;
				if (s.indexOf("*#*") != -1) {
					String[] rl = s.split("\\\\*#\\\\*");
					for (int a = 0; a < rl.length; a++) {
						if (rl[a].charAt(rl[a].length() - 1) == '*') {
							rl[a] = rl[a].substring(0,
									rl[a].length() - 1);
						}
						if (rl[a].charAt(0) == '*') {
							rl[a] = rl[a].substring(1);
						}
					}
					return rl;
				}
			}
			return value;
		}
		return value;
	}

	String unit;

	private static Class<?> getType(IPropertyMetadata m) {
		if (m != null) {
			Object value = m.getMetaPropertyValue("parser-id");
			if (value != null && value.equals("dimension")) {

				return Double.class;
			}
			if (value != null && value.equals("scalar")) {
				return Double.class;
			}
			if (value != null && value.equals("date")) {
				return Date.class;
			}
		}
		return String.class;
	}

	public IPropertyMetadata getMeta() {
		return meta;
	}


	
	public String getGroup() {
		IPropertyMetadata meta2 = meta;
		return getGroup(meta2);
	}

	public static String getGroup(IPropertyMetadata meta2) {
		Object value3 = meta2
				.getMetaPropertyValue(IPropertyConstants.CHAR_UNIT_LABEL);
		if (value3 != null) {
			return value3.toString();
		}
		Object value = meta2.getMetaPropertyValue("parser-id");
		if (value != null) {
			return (String) value;
		}
		return "";
	}

	public static String getLayoutGroup(IPropertyMetadata property, Object value) {
		String group = getGroup(property);
		if (property.id().contains("engine")){
			return "W";
		}
		if (property.id().contains("number of props")){
			return "W";
		}
		if (property.id().contains("type of prop")){
			return "W";
		}
		if (property.id().contains("type of jet")){
			return "W";
		}
		if (group.startsWith("kg")){
			return "kg";
		}
		if (group.startsWith("km/h")){
			return "Performance";
		}
		if(property.id().equals("power/mass")){
			return "W";
		}
		if (group.startsWith("m")){
			if (group.equals("m/min")){
				group="Performance";
				return group;
			}
			if (value instanceof Object[]){
				Object[]vv=(Object[]) value;
				if (vv[1] instanceof Number){
					Number z=(Number) vv[1];
					if (z.doubleValue()>1000){
						group="Performance";
						return group;
					}
				}
			}	
			if (value instanceof Number){
				Number z=(Number) value;
				if (z.doubleValue()>1000){
					group="Performance";
					return group;
				}
			}
			group="Dimensions";
		}
		return group;
	}

}
